package com.haiertvbic.tvnow.baseactivity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.haiertvbic.tvnow.R;
import com.haiertvbic.tvnow.service.ServiceUtil;
import com.haiertvbic.tvnow.service.SwitchChanellCallback;
import com.haiertvbic.tvnow.util.Constant;
import com.haiertvbic.tvnow.util.SLog;

public abstract class SwitchActivity extends BaseActivity {
	protected static final String TAG = "BaseActivity";
	private ListView objectListV;
	private boolean canSwitch = true;
	private int currentId;
	Context context;
	Handler switchHandler;
	// dtv换台失败
	public static final int DTV_CHANGE_FAILED = 10005;
	public static final int OTHER_SOURCE = 10006;
	public static final int THE_SAME_CID = 10007;
	public static final int DTV_801_CHANGE = 10008;
	public static final int GET_WHOLE_COUNTRY_ID_FAIL = 10009;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		objectListV = (ListView) findViewById(R.id.common_lv);
		switchChannel();
	}

	private void switchChannel() {
		switchHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				canSwitch = true;
				switch (msg.what) {
				case DTV_CHANGE_FAILED:
					Toast.makeText(context,
							getString(R.string.dtv_change_failed),
							Toast.LENGTH_LONG).show();
					break;
				case OTHER_SOURCE:
					Toast.makeText(context, getString(R.string.other_source),
							Toast.LENGTH_LONG).show();
					break;
				case DTV_801_CHANGE:
					Toast.makeText(context, getString(R.string.dtv_change_cid),
							Toast.LENGTH_LONG).show();
					break;
				case GET_WHOLE_COUNTRY_ID_FAIL:
					Toast.makeText(context,
							getString(R.string.get_whole_country_id_fail),
							Toast.LENGTH_LONG).show();
					break;
				case 0:
					Toast.makeText(context,
							getString(R.string.read_change_msg_failed),
							Toast.LENGTH_LONG).show();
					break;

				case 4004500:
					Toast.makeText(context, getString(R.string.change_success),
							Toast.LENGTH_LONG).show();
					currentId = Constant.CHANNELID;
					break;

				case 4004501:
					Toast.makeText(context,
							getString(R.string.channel_non_existent),
							Toast.LENGTH_LONG).show();
					break;

				case 4004502:
					Toast.makeText(context,
							getString(R.string.please_connect_crb),
							Toast.LENGTH_LONG).show();
					break;

				case 4004503:
					Toast.makeText(context, getString(R.string.no_set_crb),
							Toast.LENGTH_LONG).show();
					break;

				case -2002:
					Toast.makeText(context,
							getString(R.string.connect_break_off),
							Toast.LENGTH_LONG).show();
					break;
				default:
					break;
				}
				super.handleMessage(msg);
			}

		};
		objectListV.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				context = getApplicationContext();
				int channelId = Constant.CHANNELID;
				if (canSwitch && (currentId != channelId)) {
					canSwitch = false;
					ServiceUtil.changeChannel(context, channelId,
							new SwitchChanellCallback() {
								@Override
								public void returnData(int source,
										int resultCode, int result) {
									switchHandler.sendEmptyMessageDelayed(
											resultCode, 3000);
								}
							});

				}
			}
		});
	}

}
